<?php


namespace app\user\controller;


use app\common\controller\Api;
use app\user\model\DetailModel;
use app\user\model\OrderModel;
use app\user\model\RepairModel;
use app\user\validate\RepairValidate;
use think\Db;

class Repair extends Base
{
    protected $noNeedLogin = '*';

    public function index()
    {
        $order = OrderModel::order('id', 'DESC')
            ->field('id,image,goods_name,price,deposit,repa_switch,ddbh')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }

    public function add()
    {
        $data = input('post.');
        $validate = new RepairValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $order = OrderModel::find($data['order_id']);
        Db::startTrans();
        $add = false;
        $edit = false;
        $data2 = [
            'user_id' => $order['user_id'],
            'order_id' => $data['order_id'],
            'image' => $order['image'],
            'goods_name' => $order['goods_name'],
            'price' => $order['price'],
            'deposit' => $order['deposit'],
            'ddbh' => $order['ddbh'],
            'images' => $data['images'],
            'name' => $data['name'],
            'mobile' => $data['mobile'],
            'remarks' => $data['remarks'],
            'create_time' =>date('Y-m-d H:i:s')
        ];
        try {
            $add = RepairModel::create($data2);
            $order-> repa_switch = 1;
            $edit = $order->save();

        } catch (\Exception $exception) {
            $this->error($exception->getMessage());
            Db::rollback();
        }
//        $add = RepairModel::create($data2);
//        $order-> repa_switch = 1;
        if ($add && $edit) {
            Db::commit();
            $this->success('提交成功');
        }else{
            Db::rollback();
            $this->error('提交失败');
        }
    }


}
